package emall.token.controller;

import emall.token.config.JwtProperties;
import emall.token.service.TokenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RestController;
import token.common.pojo.UserInfo;
import token.common.util.JwtUtil;
import utils.CookieUtil;
import utils.ResponseBean;
import utils.ResultCode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@EnableConfigurationProperties(JwtProperties.class)
public class TokenController {

    @Autowired
    private TokenService authService;

    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("/award/{username}/{password}")
    public ResponseBean accredit(@PathVariable String username, @PathVariable String password, HttpServletRequest request, HttpServletResponse response){

        String token = this.authService.accredit(username,password);
        System.out.println(token);
        if (StringUtils.isBlank(token)){
            return ResponseBean.failure(ResultCode.USER_NOT_LOGGED_IN);
        }

        CookieUtil.setCookie(request,response,this.jwtProperties.getCookieName(),token,this.jwtProperties.getExpire()* 60);

        return ResponseBean.success(token);
    }
    @GetMapping("/getToken/{username}/{password}")
    public ResponseBean accredi(@PathVariable String username, @PathVariable String password, HttpServletRequest request, HttpServletResponse response){

        String token1 = this.authService.accredit(username,password);
        System.out.println(token1);
        if (StringUtils.isBlank(token1)){
            return ResponseBean.failure(ResultCode.USER_NOT_LOGGED_IN);
        }



        return ResponseBean.success(token1);
    }

    @GetMapping("/verify")
    public ResponseBean verify(@CookieValue("EMALL_TOKEN")String token, HttpServletRequest request, HttpServletResponse response){
        System.out.println("==========");
        System.out.println(token);
        System.out.println("==========");
        try {
            //通过jwt工具类使用公钥解析jwt
            UserInfo user = JwtUtil.getInfoFromToken(token,this.jwtProperties.getPublicKey());
            System.out.println(token);
            System.out.println(user);
            if (user == null){
                return ResponseBean.failure(ResultCode.USER_NOAUTHORIZED);
            }
            //刷新jwt中有效时间
            token = JwtUtil.generateToken(user,this.jwtProperties.getPrivateKey(),this.jwtProperties.getExpire());
            //刷新cookie中的有效时间
            CookieUtil.setCookie(request,response,this.jwtProperties.getCookieName(),token,this.jwtProperties.getExpire() * 60);

            return ResponseBean.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseBean.failure(ResultCode.USER_NOAUTHORIZED);
    }
}
